#include <reg52.H>
#define DS1302_SECOND		0x80
#define DS1302_MINUTE		0x82
#define DS1302_HOUR			0x84
#define DS1302_DATE			0x86
#define DS1302_MONTH		0x88
#define DS1302_DAY			0x8A
#define DS1302_YEAR			0x8C
#define DS1302_WP			0x8E

sbit DS1302_SCLK=P3^6;
sbit DS1302_IO=P3^4;
sbit DS1302_CE=P3^5;

unsigned char DS1302_Time[]={23,01,02,23,59,50};

int decimal_bcd_code(int decimal)
{
	unsigned int i;
	unsigned int sum = 0;
 
	for ( i = 0; decimal > 0; i++)
	{
		sum |= ((decimal % 10 ) << ( 4*i));
 
		decimal /= 10;
	}
 
	return sum;
}

int bcd_decimal_code( int bcd)
{
	unsigned int sum = 0, c = 1,i; 
 
	for( i = 1; bcd > 0; i++)
	{
		if( i >= 2)
		{
			c*=10;
		}
 
		sum += (bcd%16) * c;
 
		bcd /= 16; 
	}
 
	return sum;
}

void DS1302_Init(void)
{
	DS1302_CE=0;
	DS1302_SCLK=0;
}

void DS1302_WriteByte(unsigned char Command,Data)
{
	unsigned char i;
	DS1302_CE=1;
	for(i=0;i<8;i++)
	{
		DS1302_IO=Command&(0x01<<i);
		DS1302_SCLK=1;
		DS1302_SCLK=0;
	}
	for(i=0;i<8;i++)
	{
		DS1302_IO=Data&(0x01<<i);
		DS1302_SCLK=1;
		DS1302_SCLK=0;
	}
	DS1302_CE=0;
}


unsigned char DS1302_ReadByte(unsigned char Command)
{
	unsigned char i,Data=0x00;
	Command|=0x01;	
	DS1302_CE=1;
	for(i=0;i<8;i++)
	{
		DS1302_IO=Command&(0x01<<i);
		DS1302_SCLK=0;
		DS1302_SCLK=1;
	}
	for(i=0;i<8;i++)
	{
		DS1302_SCLK=1;
		DS1302_SCLK=0;
		if(DS1302_IO){Data|=(0x01<<i);}
	}
	DS1302_CE=0;
	DS1302_IO=0;	
	return Data;
}
//void DS1302_SetTime(void)
//{
//	DS1302_WriteByte(DS1302_WP,0x00);
//	DS1302_WriteByte(DS1302_YEAR,DS1302_Time[0]/10*16+DS1302_Time[0]%10);//????BCD????
//	DS1302_WriteByte(DS1302_MONTH,DS1302_Time[1]/10*16+DS1302_Time[1]%10);
//	DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
//	DS1302_WriteByte(DS1302_HOUR,DS1302_Time[3]/10*16+DS1302_Time[3]%10);
//	DS1302_WriteByte(DS1302_MINUTE,DS1302_Time[4]/10*16+DS1302_Time[4]%10);
//	DS1302_WriteByte(DS1302_SECOND,DS1302_Time[5]/10*16+DS1302_Time[5]%10);
//	DS1302_WriteByte(DS1302_DAY,DS1302_Time[6]/10*16+DS1302_Time[6]%10);
//	DS1302_WriteByte(DS1302_WP,0x80);
//}

///**
//  * @brief  DS1302????,????,DS1302?????????DS1302_Time???
//  * @param  ?
//  * @retval ?
//  */
//void DS1302_ReadTime(void)
//{
//	unsigned char Temp;
//	Temp=DS1302_ReadByte(DS1302_YEAR);
//	DS1302_Time[0]=Temp/16*10+Temp%16;//BCD????????
//	Temp=DS1302_ReadByte(DS1302_MONTH);
//	DS1302_Time[1]=Temp/16*10+Temp%16;
//	Temp=DS1302_ReadByte(DS1302_DATE);
//	DS1302_Time[2]=Temp/16*10+Temp%16;
//	Temp=DS1302_ReadByte(DS1302_HOUR);
//	DS1302_Time[3]=Temp/16*10+Temp%16;
//	Temp=DS1302_ReadByte(DS1302_MINUTE);
//	DS1302_Time[4]=Temp/16*10+Temp%16;
//	Temp=DS1302_ReadByte(DS1302_SECOND);
//	DS1302_Time[5]=Temp/16*10+Temp%16;
//	Temp=DS1302_ReadByte(DS1302_DAY);
//	DS1302_Time[6]=Temp/16*10+Temp%16;
//}
void DS1302_SetTime(void)
{
	DS1302_WriteByte(DS1302_WP,0x00);
	DS1302_WriteByte(DS1302_YEAR,decimal_bcd_code(DS1302_Time[0]));
	DS1302_WriteByte(DS1302_MONTH,decimal_bcd_code(DS1302_Time[1]));
	DS1302_WriteByte(DS1302_DATE,decimal_bcd_code(DS1302_Time[2]));
	DS1302_WriteByte(DS1302_HOUR,decimal_bcd_code(DS1302_Time[3]));
	DS1302_WriteByte(DS1302_MINUTE,decimal_bcd_code(DS1302_Time[4]));
	DS1302_WriteByte(DS1302_SECOND,decimal_bcd_code(DS1302_Time[5]));
	DS1302_WriteByte(DS1302_WP,0x80);
}

void DS1302_ReadTime(void)
{
	unsigned char Temp;
	Temp=DS1302_ReadByte(DS1302_YEAR);
	DS1302_Time[0]=bcd_decimal_code(Temp);
	Temp=DS1302_ReadByte(DS1302_MONTH);
	DS1302_Time[1]=bcd_decimal_code(Temp);
	Temp=DS1302_ReadByte(DS1302_DATE);
	DS1302_Time[2]=bcd_decimal_code(Temp);
	Temp=DS1302_ReadByte(DS1302_HOUR);
	DS1302_Time[3]=bcd_decimal_code(Temp);
	Temp=DS1302_ReadByte(DS1302_MINUTE);
	DS1302_Time[4]=bcd_decimal_code(Temp);
	Temp=DS1302_ReadByte(DS1302_SECOND);
	DS1302_Time[5]=bcd_decimal_code(Temp);
}
